<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ page import="com.smarthome.web.property.utils.PermissionUtil"  %>

<%
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<jsp:include page="${path}/views/layout/inc.jsp"></jsp:include>
<script type="text/javascript" src="${path}/js/system/account.js" ></script>
<script type="text/javascript">
	$.canEdit = true;
	$.canDelete = true;
	$.canChangeHouse=true;
	var dataGrid;
	$(function(){
		//加载小区、楼栋、单元、门牌等信息
		loadResidence('');
		var isAdmin = ${isAdmin};
		if(!isAdmin){
			$("#residenceId").combobox("select","${residenceId}");
			$("#residenceId").combobox("disable");
		}
		//加载用户状态
		loadStatus();
		dataGrid=$("#dataGrid").datagrid({
			url:"/account/dataGrid",
			width:'100%',
			nowrap: false,
			striped: true,
			fit:true,
			collapsible:true,
			pageNumber:1,
			pageSize:10,
			pageList:[10],
			fitColumns:true,
			columns:[[
				{field:'name',title:'用户姓名',width:100},
				{field:'loginName',title:'登录名',width:100},
				{field:'typeName',title:'类型',width:100},
				{field:'sex',title:'性别',width:50,formatter:function(value, row, index){
					return value==1?"男":"女";
				}},
				{field:'mobile',title:'手机号',width:100},
				{field:'statusText',title:'状态',width:100, formatter:function(value, row, index){
					var ok = "<span style='color:green'>"+value+"</span>";
					var not = "<span style='color:red'>"+value+"</span>";
					return value == '正常'?ok:not;
				}},
				{field:'gmtCreate',title:'新增时间',width:100,sortable:true,formatter : function(value, row, index) {
					var unixTimestamp = new Date(value);  
                    return unixTimestamp.format("yyyy-MM-dd hh:mm:ss");
				}},
				{field:'action',title:'操作',width:100,formatter : function(value, row, index) {
					var str = '';
					if ($.canEdit) {
						str += $.formatString('<img style="cursor:pointer" onclick="editFun(\'{0}\');" src="{1}" title="修改"/>', index, '${path}/images/extjs_icons/pencil.png');
					}
					str += '&nbsp;';
					if ($.canDelete) {
						str += $.formatString('<img style="cursor:pointer" onclick="delFun(\'{0}\');" src="{1}" title="删除"/>', index, '${path}/images/extjs_icons/cancel.png');
					}
					str += '&nbsp;';
					if($.canChangeHouse){
						str += $.formatString('<img style="cursor:pointer" onclick="editHose(\'{0}\');" src="{1}" title="分配房屋"/>', index, '${path}/images/extjs_icons/book_edit.png');
					}
					return str;
				}}
			]],
			sortName: 'gmtCreate',
			sortOrder: 'desc',
			checkOnSelect : false,
			selectOnCheck : false,
			pagination:true,
			rownumbers:true,
			singleSelect:true, // 是否单选
			toolbar:"#toolbar",
			onLoadSuccess : function() {
				parent.$.messager.progress('close');
// 				$(this).treegrid('tooltip');
			},
			onRowContextMenu : function(e, index, row){
				e.preventDefault();
				dataGrid.datagrid("unselectAll");
				dataGrid.datagrid("selectRow",index);
				$('#menu').menu('show', {
					left : e.pageX,
					top : e.pageY
				});
			}
		});
	});
	function addFun(){
		parent.$.modalDialog({
			title : '新增业主信息',
			width : 500,
			height : 350,
			href : '/account/addPage',
			buttons : [ {
				text : '确认新增',
				handler : function() {
					parent.$.modalDialog.openner_dataGrid = dataGrid;//因为新增成功之后，需要刷新这个treeGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			} ]
		});
	}
	function editFun(index){
		if(index)dataGrid.datagrid("selectRow",index);
		var row = dataGrid.datagrid('getSelected');
		if (row) {
			parent.$.modalDialog({
				title : '修改业主信息',
				width : 500,
				height : 350,
				href : '/account/editPage?id=' + row.id,
				buttons : [ {
					text : '确认修改',
					handler : function() {
						parent.$.modalDialog.openner_dataGrid = dataGrid;//因为新增成功之后，需要刷新这个treeGrid，所以先预定义好
						var f = parent.$.modalDialog.handler.find('#form');
						f.submit();
					}
				} ]
			});
		}
	}
	function delFun(index){
		if(index) dataGrid.datagrid("selectRow",index);
		var row = dataGrid.datagrid('getSelected');
		if (row) {
			if(row.status==0){
				parent.$.messager.alert('提示', "该用户已删除", 'info');return;
			}
			parent.$.messager.confirm('询问', '您是否要删除该用户？', function(b) {
				if (b) {
					parent.$.messager.progress({
						title : '提示',
						text : '数据处理中，请稍后....'
					});
					$.post('/account/delete', {
						id : row.id
					}, function(result) {
						parent.$.messager.progress('close');
						parent.$.messager.alert('提示', result.msg, 'info');
						if (result.success) {
							dataGrid.datagrid('reload');
						}
					}, 'JSON');
				}
			});
		}
	}
	
	function editHose(index){
		if(index) dataGrid.datagrid("selectRow",index);
		var row = dataGrid.datagrid('getSelected');
		if(row){
			var tname=row.name?row.name:"未设置姓名";
			parent.$.modalDialog({
				title : '分配房屋[<span style="color:red">'+tname+'</span>]',
				width : 550,
				height : 400,
				href : '/account/editHousePage?id=' + row.id
			});
		}
	}
	
	function searchFun() {
		dataGrid.datagrid('load', $.serializeObject($('#searchForm')));
	}
	function cleanFun() {
		$('#searchForm input').val('');
		dataGrid.datagrid('load', {});
	}
	
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>业主</title>
</head>
<body>
	<div class="easyui-layout" data-options="fit:true,border:false">
		<div data-options="region:'north',title:'查询条件',border:false" style="height: 140px; overflow: hidden;">
			<form id="searchForm">
				<table class="table table-hover table-condensed" style="">
					<tr>
						<td>小区</td>
						<td><input id="residenceId" name="residenceId" /></td>
						<td>楼栋</td>
						<td><input id="buildingId" name="buildingId" class="easyui-combobox" panelHeight='auto' /></td>
					</tr>
					<tr>
						<td>单元</td>
						<td><input id="unitId" name="unitId" class="easyui-combobox" panelHeight='auto' /></td>
						<td>门牌</td>
						<td><input id="doorId" name="doorId" class="easyui-combobox" panelHeight='auto' /></td>
					</tr>
					<tr>
						<td>用户状态</td>
						<td><input id="status" name="status" class="easyui-combobox" panelHeight='auto' /></td>
						<td>手机号</td>
						<td><input id="mobile" name="mobile" class="easyui-validatebox" /></td>
					</tr>
					<tr>
						<td>用户姓名</td>
						<td><input id="name" name="name" class="easyui-validatebox" /></td>
						<td>登录名</td>
						<td><input id="loginName" name="loginName" class="easyui-validatebox" /></td>
					</tr>
				</table>
			</form>
		</div>
		<div data-options="region:'center',border:false" title="" style="overflow: hidden;">
			<table id="dataGrid"> </table>
		</div>
		<div id="toolbar" style="display: none;">
				<a onclick="addFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'">新增</a>
				<a onclick="searchFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-search'">提交查询</a>
				<a onclick="cleanFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-clear'">重置查询</a>
		</div>
	</div>
	<div id="menu" class="easyui-menu" style="width: 120px; display: none;">
		<div onclick="addFun();" data-options="iconCls:'icon-add'">增加</div>
		<div onclick="editFun();" data-options="iconCls:'icon-edit'">修改</div>
		<div onclick="delFun();" data-options="iconCls:'icon-delete'">删除</div>
	</div>
</body>
</html>